/*
 * File: SendMessageTest.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package servlets.tests;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;

import javax.servlet.ServletException;

import org.junit.Test;

import servlets.cron.SendMessages;
import tests.AppEngineTest;
import tests.HttpServletRequestMock;
import tests.HttpServletResponseMock;

import faceBook.Message;
import faceBook.User;
import faceBook.tests.helper.TestUsers;

/**
 * The Class SendMessageTest.
 */
@SuppressWarnings("static-method")
public class SendMessageTest extends AppEngineTest
{
  
  /**
   * Tests the getAllMessageForDay method.
   * 
   * @throws ServletException
   *           the servlet exception
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  @Test
  public void testSendMessages() throws ServletException, IOException
  {
    User test0 = TestUsers.getTestUser(0);
    test0.setBirthday(new GregorianCalendar().getTimeInMillis());
    test0.save();
    User test1 = TestUsers.getTestUser(1);
    test1.setBirthday(new GregorianCalendar().getTimeInMillis());
    test1.setUseDefaultBD(true);
    test1.setDefaultBDMessageMale("Hey *FIRST_NAME* *LAST_NAME*, Happy Birthday *FIRST_NAME*");
    test1.setDefaultBDMessageFemale("Hey *FIRST_NAME* *LAST_NAME*, Happy Birthday *FIRST_NAME*");
    test1.save();
    
    GregorianCalendar today = new GregorianCalendar();
    Message message = new Message();
    message.setSendDate(today.getTimeInMillis());
    message.setUser(TestUsers.getTestUser(0));
    message.addFriend(TestUsers.getTestUser(1));
    message.setMessage("First Message");
    message.setLink("");
    message.save();
    Message message2 = new Message();
    today.add(Calendar.DATE, 1);
    message2.setSendDate(today.getTimeInMillis());
    message2.setUser(TestUsers.getTestUser(0));
    message2.addFriend(TestUsers.getTestUser(1));
    message2.setMessage("Second Message");
    message2.setLink("");
    message2.save();
    Message message3 = new Message();
    today.add(Calendar.DATE, -2);
    message3.setSendDate(today.getTimeInMillis());
    message3.setUser(TestUsers.getTestUser(0));
    message3.addFriend(TestUsers.getTestUser(1));
    message3.setMessage("Third Message");
    message3.setLink("");
    message3.save();
    assertEquals("", message.getConfirmations());
    SendMessages sm = new SendMessages();
    sm.doGet(new HttpServletRequestMock(new HashMap<String, String>()),
        new HttpServletResponseMock());
    message.load();
    message2.load();
    message3.load();
    message.delete();
    message2.delete();
    message3.delete();
    assertEquals("", message2.getConfirmations());
    assertEquals("", message3.getConfirmations());
    assertNotSame("", message.getConfirmations());
    
    ArrayList<Message> Messages = Message.getAllMessagesForUser(test1);
    for (Message m : Messages)
    {
      m.delete();
      assertEquals("Hey New Hack, Happy Birthday New", m.getMessage());
    }
  }

}
